package com.yanqu.road.dao.impl.activity.oldplayerreturn;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.oldplayerreturn.OldPlayerReturnShopLimit;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OldPlayerReturnShopLimitDaoImpl extends TempDataObjectDao<OldPlayerReturnShopLimit> {
    public static String getOldPlayerReturnShopLimitSql = "select * from `t_u_return_player_add_shop_limit` where `userId`=?";

    public Map<Integer, OldPlayerReturnShopLimit> getOldPlayerReturnShopLimit(long userId) {
        Map<Integer, OldPlayerReturnShopLimit> addShopLimitMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getOldPlayerReturnShopLimitSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                OldPlayerReturnShopLimit limit = parse(rs);
                addShopLimitMap.put(limit.getMallId(), limit);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return addShopLimitMap;
    }

    public static String updateOldPlayerReturnShopLimitSql = "update `t_u_return_player_add_shop_limit` set totalAddLimit=?,hasBuyTimes=? where userId=? and mallId=?";

    @Override
    protected boolean updateData(OldPlayerReturnShopLimit oldPlayerReturnShopLimit) {
        oldPlayerReturnShopLimit.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, oldPlayerReturnShopLimit.getTotalAddLimit()));
        para.put(i++, new DbParameter(Types.INTEGER, oldPlayerReturnShopLimit.getHasBuyTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, oldPlayerReturnShopLimit.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, oldPlayerReturnShopLimit.getMallId()));
        if (getDb().executeNoneQuery(updateOldPlayerReturnShopLimitSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertOldPlayerReturnShopLimitSql = "insert into `t_u_return_player_add_shop_limit`(userId,mallId,totalAddLimit,hasBuyTimes) values (?,?,?,?)";

    @Override
    protected boolean addData(OldPlayerReturnShopLimit oldPlayerReturnShopLimit) {
        oldPlayerReturnShopLimit.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, oldPlayerReturnShopLimit.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, oldPlayerReturnShopLimit.getMallId()));
        para.put(i++, new DbParameter(Types.INTEGER, oldPlayerReturnShopLimit.getTotalAddLimit()));
        para.put(i++, new DbParameter(Types.INTEGER, oldPlayerReturnShopLimit.getHasBuyTimes()));
        if (getDb().executeNoneQuery(insertOldPlayerReturnShopLimitSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected OldPlayerReturnShopLimit parseData(ResultSet rs) throws SQLException {
        OldPlayerReturnShopLimit oldPlayerReturnShopLimit = new OldPlayerReturnShopLimit();
        oldPlayerReturnShopLimit.setUserId(rs.getLong("userId"));
        oldPlayerReturnShopLimit.setMallId(rs.getInt("mallId"));
        oldPlayerReturnShopLimit.setTotalAddLimit(rs.getInt("totalAddLimit"));
        oldPlayerReturnShopLimit.setHasBuyTimes(rs.getInt("hasBuyTimes"));

        return oldPlayerReturnShopLimit;
    }
}
